package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.exception.ServiceException;
import com.member.common.utils.StringUtils;
import com.member.common.utils.bean.BeanUtils;
import com.member.shop.domain.SysBizOtherUser;
import com.member.shop.mapper.SysBizOtherUserMapper;
import com.member.shop.service.ISysBizOtherUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 用户层级Service业务层处理
 *
 * @author wangxin
 * @date 2024-06-11
 */
@Service
public class SysBizOtherUserServiceImpl extends ServiceImpl<SysBizOtherUserMapper, SysBizOtherUser> implements ISysBizOtherUserService {
    @Resource
    private SysBizOtherUserMapper sysBizOtherUserMapper;

    /**
     * 查询用户层级
     *
     * @param id 用户层级主键
     * @return 用户层级
     */
    @Override
    public SysBizOtherUser selectSysBizOtherUserById(Long id) {
        return sysBizOtherUserMapper.selectById(id);
    }

    /**
     * 查询用户层级列表
     *
     * @param sysBizOtherUser 用户层级
     * @return 用户层级
     */
    @Override
    public List<SysBizOtherUser> selectSysBizOtherUserList(LambdaQueryWrapper<SysBizOtherUser> queryWrapper) {
        return sysBizOtherUserMapper.selectList(queryWrapper);
    }

    /**
     * 新增用户层级
     *
     * @param sysBizOtherUser 用户层级
     * @return 结果
     */
    @Override
    public int insertSysBizOtherUser(SysBizOtherUser sysBizOtherUser) {
        return sysBizOtherUserMapper.insert(sysBizOtherUser);
    }

    /**
     * 修改用户层级
     *
     * @param sysBizOtherUser 用户层级
     * @return 结果
     */
    @Override
    public int updateSysBizOtherUser(SysBizOtherUser sysBizOtherUser) {
        return sysBizOtherUserMapper.updateById(sysBizOtherUser);
    }

    /**
     * 批量删除用户层级
     *
     * @param ids 需要删除的用户层级主键
     * @return 结果
     */
    @Override
    public int deleteSysBizOtherUserByIds(Long[] ids) {
        return sysBizOtherUserMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除用户层级信息
     *
     * @param id 用户层级主键
     * @return 结果
     */
    @Override
    public int deleteSysBizOtherUserById(Long id) {
        return sysBizOtherUserMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String importUser(List<SysBizOtherUser> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.isEmpty()) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        ArrayList<SysBizOtherUser> saveList = new ArrayList<>();
        for (SysBizOtherUser user : userList) {
            try {
                List<SysBizOtherUser> sysBizOtherUsers = sysBizOtherUserMapper.selectList(new LambdaQueryWrapper<SysBizOtherUser>().eq(SysBizOtherUser::getUserCode, user.getUserCode()));
                if (!CollectionUtils.isEmpty(sysBizOtherUsers)) {
                    SysBizOtherUser sysBizOtherUser = new SysBizOtherUser();
                    Long id = sysBizOtherUsers.get(0).getId();
                    BeanUtils.copyProperties(user, sysBizOtherUser);
                    sysBizOtherUser.setId(id);
                    saveList.add(sysBizOtherUser);
//                    sysBizOtherUserMapper.updateById(sysBizOtherUser);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、账号 ").append(user.getUserCode()).append(" 更新成功");
                    continue;
                }
                saveList.add(user);
                sysBizOtherUserMapper.insert(user);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、账号 ").append(user.getUserCode()).append(" 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getUserCode() + " 导入失败：";
                failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(user.getUserCode()).append(" 导入失败");
                log.error(msg, e);
            }
        }
        if (!CollectionUtils.isEmpty(saveList)) {
            saveOrUpdateBatch(saveList);
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
